home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / utils / ptwist.exe / POVTWIST.C next >
Text File  |  1993-09-04  |  6KB  |  181 lines

  1. /******************************************************************
  2.  * 
  3.  * Program : Povtwist.c
  4.  * Purpose : Create twisted objects for the POVRAY raytracer
  5.  * Created : 12/05/90
  6.  * By      : Drew Wells [CIS 73767,1244] originally for DKBtrace
  7.  * POV VERS: James P. Hawkins [CIS 76520,3356]
  8.  * Files   : Povtwist.c
  9.  * Compiler: Microsoft C 7.00
  10.  * Model   : Medium
  11.  * Comments: This simple program used to create the floating twisters in
  12.  *          the image "Not a Trace of Reality" a.k.a. Ntreal.gif.
  13.  *          It should be compatible with any ANSI C compiler, and is 
  14.  *          easily modifiable to create more complex shapes by adding
  15.  *          code to change the x and z axis rotation and step.
  16.  *
  17.  *
  18.  * Copyright 1990 Drew Wells
  19.  * This copyrighted code is released for non-commercial use only.
  20.  * It may not be sold or used a part of a commercial package.
  21.  * This code may be modified and distributed provided it is done at no
  22.  * cost to the user.
  23.  * Please comment and maintain version list if you modify this code.
  24.  ******************************************************************/
  25. /* Version
  26.  *   Twister 0.01 12/05/90 DW Created program.   
  27.  *   Twister 1.00 12/26/90 DW Added user input.
  28.  *   Povtwist  1.00 09/04/93 JPH POVRAY version derived from Twister
  29.  *
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <conio.h>
  35.  
  36. void show_title(void); 
  37. void get_parameters(void);
  38. void write_header(char *filename, char *union_name,  char *objname);
  39. void write_quadric(char *objname);
  40. void write_piece(char *objname,double xpos, double ypos, double zpos,
  41.                                double xrot, double yrot, double zrot);
  42. void write_end(char *filename, char *union_name);
  43. void err_exit(char *message);
  44.  
  45. FILE *outfile;
  46. char object[80],filename[80],union_name[80],workstr[256];
  47. double length, numtwist, ystep, rstep,xrot,yrot,zrot,x,y,z,numdeg;
  48. long numpieces;
  49.  
  50. main()
  51. {  
  52.    show_title();
  53.    
  54. /* Get twister values from user */
  55.     get_parameters();
  56.  
  57. /* Open file for output  and write header */
  58.     printf("\n\nCreating POV data file %s...\n", filename);
  59.     if ((outfile = fopen(filename,"w")) == NULL)
  60.      err_exit("Opening file.");
  61.     
  62.     write_header(filename,union_name, object);
  63.     
  64. /* Create twister and write each piece to file */ 
  65.     for(y=0.0;y<=length;y+=ystep){
  66.       write_piece(object,x,y,z,xrot,yrot,zrot);
  67.       yrot += rstep;
  68.      if(yrot>=360.0)
  69.          yrot = yrot - 360.0;
  70.      }
  71.  
  72. /* Write object end and close file */
  73.     write_end(filename,union_name);
  74.     fclose(outfile);
  75.     printf("\nPOV data file %s created.\n", filename);
  76.     return(0);
  77. }
  78.  
  79.  
  80. void show_title(void)
  81.    printf("\n\n\n\n");
  82.    printf("___________________________________________________________________\n\n");
  83.    printf("[ POVTWIST V1.0 ]\n");
  84.    printf("This program creates a POVRAY raytracer data file of a twisted object.\n"); 
  85.    printf("See POVTWIST.doc for more details.\n");
  86.    printf("- DW 12/26/90, JPH 09/04/93\n");
  87.    printf("___________________________________________________________________\n");
  88. }
  89.  
  90. void get_parameters(void)
  91. /* Get twister values from user */    
  92.     
  93.     printf("Twister filename? [twist.pov]: ");
  94.     gets(workstr);
  95.     strcpy(filename,workstr);
  96.     
  97.     printf("Union name? [Macaroni]: ");
  98.     gets(workstr);
  99.     strcpy(union_name,workstr);
  100.     
  101.     printf("Quadric name? [Part]: ");
  102.     gets(workstr);
  103.     strcpy(object,workstr);
  104.     
  105.     
  106.     printf("Length of twister? [20.0]: ");
  107.     gets(workstr);
  108.     length = _atold(workstr);
  109.     
  110.     printf("Number of pieces for twister? [50]: ");
  111.     gets(workstr);
  112.     numpieces = atoi(workstr);
  113.     
  114.     printf("Number of many twists? [1.0]: ");
  115.     gets(workstr);
  116.     numtwist = _atold(workstr);
  117.  
  118.  /* Set up default values */
  119.     if(filename[0]=='\0')
  120.         strcpy(filename, "twist.pov");
  121.     if(union_name[0]=='\0') strcpy(union_name,"Macaroni");
  122.     if(object[0]=='\0') strcpy(object,"Part");
  123.     if(length==0.0) length = 20.0;
  124.     if(numpieces==0) numpieces = 50;
  125.     if(numtwist==0.0) numtwist = 1.0;
  126.     
  127.     xrot = yrot = zrot = x = y = z = 0.0;
  128.     ystep = length/(double)(numpieces-1);
  129.     rstep = (360.0 * numtwist)/(length/ystep); 
  130. }
  131.  
  132. void write_header(char *filename,  char *union_name, char *objname)
  133.   fprintf(outfile,"/* File : %s  Union Name : %s */\n", filename, union_name);
  134.   fprintf(outfile,"/* This data file created by POVTWIST for the POVRAY ray tracer. */\n");
  135.   fprintf(outfile,"/* NOTE: Change quadric '%s' to change the look of '%s' */\n",
  136.     objname, union_name); 
  137.   fprintf(outfile,"/* See TWISTER docs for more details.*/\n\n");
  138.   write_quadric(objname);
  139.   fprintf(outfile,"\n/* Twister %s Length=%.3lf Number pieces=%ld Number twists=%.3lf */\n",
  140.     union_name,length,numpieces,numtwist);
  141.   fprintf(outfile,"#declare %s = object { \n union {\n",union_name);
  142. }
  143.  
  144. void write_quadric(char *objname)
  145.   /* Write a sample quadric to file for ease of use */
  146.  
  147.       fprintf(outfile,"#declare %s = quadric { Sphere ", objname);
  148.       fprintf(outfile,"scale < %.2lf 1.0 1.0 > }\n", length/4.0);
  149. }  
  150. void write_piece(char *objname,double xpos, double ypos, double zpos,
  151.                             double xrot, double yrot, double zrot)
  152. {
  153.     char tofile[256];
  154.     
  155.     sprintf(tofile,"   quadric { %s\n   rotate <%.3lf %.3lf %.3lf> translate <%.3lf %.3lf %.3lf>\n   }\n",
  156.                 objname,xrot,yrot,zrot,xpos,ypos,zpos);
  157.  
  158.     if( fputs(tofile,outfile) )
  159.      err_exit("Writing string to file.");
  160.     
  161. }
  162.   
  163. void write_end(char *filename, char *union_name)
  164. {
  165.   fprintf(outfile," }\n texture { color red 0.9 green 0.1 blue 0.1}\n}\n");
  166.   fprintf(outfile,"/* End_File : %s  End_Union Name : %s */\n",filename,union_name);
  167. }
  168.  
  169. void err_exit(char *message)
  170. {
  171.     puts("\n\nERROR! \a");
  172.     puts(message);
  173.     puts("- Exiting program\n[Press Any Key]");
  174.     getch();
  175.     exit(1);
  176. }
  177.